package com.group9.service.impl;

import com.group9.params.ElasticInfoQuery;
import com.group9.pojo.ElasticFruitInfo;
//import com.group9.repository.ElasticFruitInfoRepository;
import com.group9.service.ElasticFruitInfoService;
import org.springframework.data.domain.Page;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by QZP on 2020/5/27.
 */
//@Service
//public class ElasticFruitInfoServiceImpl implements ElasticFruitInfoService {

//    @Autowired
//    ElasticsearchTemplate elasticsearchTemplate;
//
//    @Autowired
//    ElasticFruitInfoRepository elasticFruitInfoRepository;
//
//    @Override
//    public void saveAllElasticInfo(List<ElasticFruitInfo> elasticFruitInfoList) {
//        boolean isExist = elasticsearchTemplate.indexExists(ElasticFruitInfo.class);
//        if (!isExist) {
//            elasticsearchTemplate.createIndex(ElasticFruitInfo.class);
//        }
//        elasticFruitInfoRepository.saveAll(elasticFruitInfoList);
//    }
//
//    @Override
//    public Iterable<ElasticFruitInfo> getAll() {
//        return elasticFruitInfoRepository.findAll();
//    }
//
//    @Override
//    public List<ElasticFruitInfo> findByFruitPriceBetween(double low, double up) {
//        return elasticFruitInfoRepository.findByFruitPriceBetween(low,up);
//    }
//
//    @Override
//    public List<ElasticFruitInfo> findByFruitName(String fruitName) {
//        return elasticFruitInfoRepository.findByFruitName(fruitName);
//    }
//
//    @Override
//    public Page<ElasticFruitInfo> ikSearch(String field, String queryString) {
//        NativeSearchQueryBuilder queryBuilder = new NativeSearchQueryBuilder();
//        queryBuilder.withQuery(QueryBuilders.matchQuery(field,queryString));
//        Page<ElasticFruitInfo> elasticFruitInfos = elasticFruitInfoRepository.search(queryBuilder.build());
//        return elasticFruitInfos;
//    }
//
//    @Override
//    public Page<ElasticFruitInfo> ikSearchByPage(ElasticInfoQuery elasticInfoQuery) {
//        NativeSearchQueryBuilder queryBuilder = new NativeSearchQueryBuilder();
//        queryBuilder.withQuery(QueryBuilders.termQuery(elasticInfoQuery.getField(),elasticInfoQuery.getValue()));
//        queryBuilder.withPageable(PageRequest.of(elasticInfoQuery.getCurrPage(),elasticInfoQuery.getPageSize()));
//
//        Page<ElasticFruitInfo> elasticFruitInfos = elasticFruitInfoRepository.search(queryBuilder.build());
//        return elasticFruitInfos;
//    }
//}
